package com.wisdytech.linkdcs.equipLifeCycle.maintain.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dao.RepairManageDao;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.RepairManage;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.service.IRepairManageService;
import com.wisdytech.linkdcs.system.dao.ISysUserDao;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhanghailin
 * @since 2018-07-19
 */
@Service
public class RepairManageServiceImpl extends BaseService<RepairManageDao, RepairManage> implements IRepairManageService {

    @Resource
    private RepairManageDao repairManageDao;

    @Resource
    private ISysUserDao sysUserDao;

    @Override
    public PageInfo<RepairManage> findRepairManageByPage(String equipCode, String equipName, String maintenanceProject, String dutyDepartment, String maintenanceStartDate, String maintenanceEndDate, Filter filter) {
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(equipCode)) {
            params.put("equipCode",equipCode);
        }
        if(StringUtils.isNotBlank(equipName)) {
            params.put("equipName",equipName);
        }
        if(StringUtils.isNotBlank(maintenanceProject)) {
            params.put("maintenanceProject",maintenanceProject);
        }
        if(StringUtils.isNotBlank(dutyDepartment)) {
            params.put("dutyDepartment",dutyDepartment);
        }
        if(StringUtils.isNotBlank(maintenanceStartDate)) {
            params.put("maintenanceStartDate",maintenanceStartDate);
        }
        if(StringUtils.isNotBlank(maintenanceEndDate)) {
            params.put("maintenanceEndDate",maintenanceEndDate);
        }
        List<RepairManage> list = super.beforeList(RepairManageDao.class,filter).selectRepairManageList(params);
        list.forEach(repairManage -> {
            //查询用户名称
            if (StringUtils.isNotBlank(repairManage.getRepairUser())) {
                String[] usernames = repairManage.getRepairUser().split(",");
                String truenames = this.repairManageDao.selectTruenamesByUsernames(usernames);
                repairManage.setRepairUser(truenames);
            }
        });
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdateRepairManage(RepairManage repairManage, SysUser operator) {
        if (operator == null) {
            throw new ServiceException("操作人不能为空");
        }
        Optional<RepairManage> repairManageOptional = Optional.ofNullable(repairManage);
        repairManageOptional.orElseThrow(() -> new ServiceException("参数不能为空"));
        repairManageOptional
                .map(RepairManage::getEquipCode)
                .filter(StringUtils::isNotBlank)
                .orElseThrow(() -> new ServiceException("设备不能为空"));

        if (StringUtils.isNotBlank(repairManage.getPkId())) {
            repairManage.setUpdateDate(new Date());
            repairManage.setUpdateUser(operator.getUsername());
            repairManageDao.updateById(repairManage);
        } else {
            repairManage.setPkId(BaseUtil.getUUID());
            repairManage.setCreateDate(new Date());
            repairManage.setCreateUser(operator.getUsername());
            repairManage.setDelFlag(CommonConstants.DEL_FLAG);
            repairManageDao.insert(repairManage);
        }

    }

    @Override
    public RepairManage findRepairManageById(String pkId) {
        RepairManage repairManage = this.repairManageDao.selectRepairManageById(pkId);
        if (repairManage == null) {
            return null;
        }
        if (StringUtils.isNotBlank(repairManage.getRepairUser())) {
            String[] usernames = repairManage.getRepairUser().split(",");
            EntityWrapper<SysUser> ew = new EntityWrapper<>();
            ew.setSqlSelect("pk_id","username","truename").in("username",usernames);
            List<SysUser> userList = sysUserDao.selectList(ew);
            repairManage.setUserList(userList);
        }
        return repairManage;
    }

    @Override
    public void logicDeleteRepairManageByPkIds(String pkIds) {
        if (StringUtils.isBlank(pkIds))
            return;
        String[] pkIdArray = pkIds.split(",");
        RepairManage repairManage = new RepairManage();
        repairManage.setDelFlag(CommonConstants.DEL_FLAG_TRUE);
        Arrays.stream(pkIdArray).forEach(pkId -> {
            repairManage.setPkId(pkId);
            repairManageDao.updateById(repairManage);
        });
    }
}
